home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 February / Macworld (1999-02).dmg / Shareware World / Shareware Feature / PageSpinner 2.0.1 / Examples / JavaScript / Dynamic Document Stationery < prev    next >
Text File  |  1997-06-17  |  6KB  |  212 lines

  1. <HTML>
  2. <HEAD>
  3. <TITLE>Dynamic Document</TITLE>
  4.  
  5. <SCRIPT LANGUAGE="JavaScript">
  6. <!-- Beginning of JavaScript --------
  7. /*
  8.     Dynamic Document
  9.     Written by Jerry Aman, Optima System, Aug 2, 1996. 
  10.     Modified Aug 13, 1996
  11.  
  12.     September 24: Added a tweak that will make the time 
  13.     to be displayed correctly in Navigator 3.0.
  14.     The time seem to be offset by one hour, maybe this have
  15.     something to do with daylight savings?
  16.  
  17.     Part of the PageSpinner distribution.
  18.  
  19.     We will not be held responsible for any unwanted 
  20.     effects due to the usage of this script or any derivative.  
  21.     No warrantees for usability for any specific application are 
  22.     given or implied.
  23.  
  24.     You are free to use and modify this script,
  25.     if the credits above are given in the source code
  26. */
  27.  
  28. // getHourOfDay()
  29. // Returns: Integer containing the hour 0-24
  30.  
  31. function getHourOfDay()
  32. {       var now = new Date();
  33.         return(now.getHours());
  34. }
  35.  
  36.  
  37. // getTime()
  38. // Returns: Text containing the time in the format HH:MM
  39.  
  40. function getTime()
  41. {       
  42.     var now = new Date();
  43.     var minutes = now.getMinutes();
  44.     var divider = ":";
  45.  
  46.     if (minutes<10)
  47.         divider = ":0";
  48.  
  49.         // Hack to get it to display the time 
  50.         // correctly in version 3.0, (adjust for offset)
  51.     if (navigator.appVersion.lastIndexOf('3.') != -1 )
  52.         return( now.getHours()-1 + divider + minutes );
  53.  
  54.         // Other versions may work with this ?    
  55.     return( now.getHours() + divider + minutes );
  56. }
  57.  
  58.  
  59. // isMacintoshBrowser()
  60. // Returns: Boolean value, true if the browser is running under MacOS
  61.  
  62. function isMacintoshBrowser()
  63.     return(navigator.appVersion.lastIndexOf('Mac') != -1 );
  64. }
  65.  
  66.  
  67. // sayHello ()
  68. // Inserts text dynamically in the document when it is called
  69. // Note that all text are dynamically inserted into the document
  70. // when a call to  this function are made in the BODY part of the file.
  71.  
  72. function sayHello ()
  73. {
  74.  
  75.  
  76.     document.write(    "I see that the time is <B>" + 
  77.                 getTime() + 
  78.                 "</B>, so I wish you" );
  79.  
  80.  
  81.     if(getHourOfDay()<5 || getHourOfDay()>19)
  82.         document.write('<FONT COLOR="2C396D"> a good night!</FONT>');
  83.     else 
  84.     {
  85.         if ( getHourOfDay() <11) 
  86.         {
  87.             document.write('<FONT COLOR="52A553"> a good morning!</FONT>');
  88.         } 
  89.         else 
  90.         { 
  91.             document.write('<FONT COLOR="ED363C"> a good day!</FONT>');
  92.         }
  93.     }
  94. }
  95.  
  96. // -- End of JavaScript code -------------- -->
  97. </SCRIPT>
  98.  
  99. </HEAD>
  100. <BODY BGCOLOR=FFFFFF TEXT=000000>
  101. <H1>JavaScript Dynamic Document</H1>
  102.  
  103. <B>This stationery page contains JavaScript that creates a dynamic page</B>
  104. <P>
  105. Please note that JavaScript is currently only available in Netscape Navigator 2.0 or higher. <BR>
  106. <FONT COLOR="931B15">Do not assume that all in your audience are using a JavaScript enabled browser.</FONT>
  107. <HR>
  108. Here is an example on how JavaScript can be used to provide a dynamic page that will change its contents depending upon the local time specified in the browser's system and the OS that is used by the reader.
  109. <P>
  110.  
  111. <!-- 1) Let the reader know the browser ID used
  112.       (this is often logged by servers for keeping statistics) -->
  113.  
  114. Welcome <B><SCRIPT>
  115. <!--
  116. document.write(navigator.userAgent)
  117. // -->
  118. </SCRIPT></B>!
  119. <P>
  120.  
  121.  
  122. <!-- 2) Tell the name of the browser used -->
  123.  
  124. <SCRIPT>
  125. <!--
  126. document.write("Nice to see that you use ", navigator.appName," ", navigator.appVersion, ".")
  127. // -->
  128. </SCRIPT>
  129. <P>
  130.  
  131.  
  132. <!-- 3) Say something that depends upon the readers local time -->
  133. <!-- This is all done in the function sayHello -->
  134.  
  135. <SCRIPT>
  136. <!--
  137. sayHello()
  138. // -->
  139. </SCRIPT>
  140.  
  141.  
  142. <!-- 4) Finally, say something special to the MacOS users -->
  143.  
  144. <SCRIPT>
  145. <!--
  146.  
  147. if (isMacintoshBrowser())
  148.     document.write(
  149.     '<P><FONT COLOR="385FD1">Very, very nice to see that you are using MacOS!</FONT>'); 
  150. else
  151.     document.write(
  152.     '<P>Sad too see that you are not using a Mac to view this page.')
  153. // -->
  154. </SCRIPT>
  155.  
  156. <HR>
  157.  
  158. <P>
  159. <B>How to use:</B><BR>
  160. Four small scripts are embedded inside the text contents of this file. The scripts are executed - and inserts text - when the page is being loaded. View the source file to see how this is done. Every function is commented.
  161. <P>
  162. Replace the contents inside the script with calls to your own test functions and add text you want to have displayed inside <FONT COLOR="FF3366"><CODE>document.write()</CODE></FONT> function inside the script. It is recommended to create functions  for complex scripts and place these in the HEAD section, see the <TT>sayHello</TT> function for an example of this. 
  163.  
  164. <P>
  165. Make sure that all text is placed inside the <FONT COLOR="FF3366"><CODE>document.write()</CODE></FONT> function, otherwise it will not look good on browsers that don't support JavaScript.
  166.  
  167. <P>
  168. Edit this page or copy selected scripts to create your own dynamic page!
  169. <P>
  170. <HR>
  171. <P>
  172. <B>Example code for the first dynamic text above:</B>
  173. <XMP>Welcome <B><SCRIPT>
  174. <!--
  175. document.write(navigator.userAgent)
  176. // -->
  177. </SCRIPT></B>!</XMP>
  178.  
  179. Note that the text "<B>Welcome !</B>" is the only text that will be displayed in browsers that don't support JavaScript.
  180. <P>
  181.  You <I>may</I> also write the script without the <FONT COLOR="555555"><!--</FONT> comment (see below), but if you do this the actual script may show up in a browser that doesn't support JavaScript. This is not recommended:
  182. <FONT COLOR="FF3366"><XMP>Welcome, <B><SCRIPT>document.write(navigator.userAgent);</SCRIPT></B>!
  183. </XMP></FONT>
  184.  
  185. <P>
  186. <HR>
  187. <P>
  188. <B>This function located in the HEAD section returns true if the browser is running on a Mac:</B><BR>
  189.  
  190. <XMP>function isMacintoshBrowser()
  191.   return(navigator.appVersion.lastIndexOf('Mac') != -1 );
  192. }
  193. </XMP>
  194.  
  195. The function can be used inside a script like this:
  196. <XMP>if (isMacintoshBrowser())
  197.   document.write('Mac specific text...'); 
  198. else
  199.   document.write('Text for other platforms...')
  200. </XMP>
  201. <P>
  202. <HR>
  203. <P>
  204. <B>Bug note:</B><BR>
  205. In this version of the script I have modified the getTime() so that it will make the time to be displayed correctly in Navigator 3.0 (and 2.0). The time seem to be offset by one hour in the 3.0 version compared to the 2.0 version! This may have something to do with daylight saving time (or maybe not...).
  206.  
  207. <P>
  208. </BODY>
  209. </HTML>
  210.